import { resumeGeneralInterface } from "@/api/job-detail.js";
import myResumeDeleteIcon from "@/assets/images/png/myResumeDeleteIcon.png";
import myResumeEditIcon from "@/assets/images/png/myResumeEditIcon.png";
import resumeItemAddIcon from "@/assets/images/png/resumeItemAddIcon.png";
export default {
  props: {
    resumeList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      personId: localStorage.getItem("personId"),
      myResumeDeleteIcon,
      myResumeEditIcon,
      resumeItemAddIcon,
      cascaderProps: {
        checkStrictly: true, // 严格检查，不自动勾选父级
        value: "value",
        label: "label",
        children: "children",
        emitPath: false, // 不返回完整的路径，只返回最终选中的值
      },
      cascaderVisibleStates: {
        aimInfo_tradeid_cascader: false,
        aimInfo_zwid_cascader: false,
        aimInfo_intended_city_cascader: false,
        basicInfo_hkb_cascader: false,
        basicInfo_hka_cascader: false,
        educationInfo_zyeArr_cascader: false,
        workInfo_jtzwtype_cascader: false,
      },
    };
  },

  methods: {
    resumeGeneralInterface(param, resolveCallback, rejectCallback) {
      this.$store.dispatch("myResume/changeSetting", {
        key: "loading",
        value: true,
      });
      const params = param;
      resumeGeneralInterface(params)
        .then((res) => {
          if (res.code == 200) {
            //console.log("param.func", param.func);
            if (param.func == "getTradeZwCache") {
              //不需要刷新数据的请求
              resolveCallback(res);
              return;
            } else {
              if (typeof resolveCallback === "function") {
                //如果你要覆盖内容，注意顺序
                this.$emit("reqGetResumeData");
                resolveCallback(res);
              }
            }
          } else {
            this.$message.error(res.status_desc || "请求失败，请稍后重试");
            if (typeof rejectCallback === "function") {
              rejectCallback();
            }
          }
        })
        .catch((e) => {
          if (typeof rejectCallback === "function") {
            rejectCallback();
          }
          this.$message.error(res.status_desc || "请求失败，请稍后重试");
        })
        .finally(() => {
          this.$store.dispatch("myResume/changeSetting", {
            key: "loading",
            value: false,
          });
        });
    },
    //检查resumeList中是否已经有正在编辑的项，返回索引
    checkEditingItem() {
      let index = -1;
      for (let i = 0; i < this.resumeList.length; i++) {
        if (this.resumeList[i].editStatus) {
          index = i;
          break;
        }
      }
      return index;
    },
    handleAddClick() {
      //判断是否最后一个已经是新的
      if (
        this.resumeDataList.length > 0 &&
        this.resumeDataList[this.resumeDataList.length - 1].isNew
      ) {
        // const edittingBlock = document.querySelector("#edittingBlock");
        // this.scrollToElement(edittingBlock);
        this.$message.error("存在未保存的项目！");
        return;
      }
      //如果已经有正在编辑的项目，则弹出提示框
      let editingIndex = this.checkEditingItem();
      if (editingIndex == -1) {
        this.resumeDataList.push({
          editStatus: true,
          editForm: {},
          isNew: true,
        });
        this.$emit("update:editStatus", true);
      } else {
        this.$confirm("有内容没有保存，确定退出编辑吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            //全部关闭编辑再新增
            this.resumeList.forEach((item) => {
              item.editStatus = false;
            });
            this.resumeDataList.forEach((item) => {
              item.editStatus = false;
            });
            this.resumeDataList.push({
              editStatus: true,
              editForm: {},
              isNew: true,
            });
            this.$emit("update:editStatus", true);
          })
          .catch(() => {});
      }
    },
    changeEditStatus(item, status) {
      if (status) {
        let editingIndex = this.checkEditingItem();
        if (editingIndex != -1) {
          this.$confirm("有内容没有保存，确定退出编辑吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              //全部关闭编辑再打开当前的
              this.resumeList.forEach((item) => {
                item.editStatus = false;
              });
              this.resumeDataList.forEach((item) => {
                item.editStatus = false;
              });
              item.editStatus = status;
              this.$emit("update:editStatus", status);
            })
            .catch(() => {});
        } else {
          item.editStatus = status;
          this.$emit("update:editStatus", status);
        }
      } else {
        //如果是设置为false则直接设置当前的状态
        item.editStatus = status;
        this.$emit("update:editStatus", status);
      }
    },
    changeEditStatusForSingle(status) {
      //当想设置editStatus为true时，检查resumeList是否已经有正在编辑的项目，如果有就弹出提示框
      if (status) {
        let editingIndex = this.checkEditingItem();
        if (editingIndex != -1) {
          this.$confirm("有内容没有保存，确定退出编辑吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              //全部关闭编辑再打开当前的
              this.resumeList.forEach((item) => {
                item.editStatus = false;
              });
              this.$emit("update:editStatus", status);
            })
            .catch(() => {});
        } else {
          this.$emit("update:editStatus", status);
        }
      } else {
        //如果是设置为false则直接设置当前的状态
        this.$emit("update:editStatus", status);
      }
    },
    scrollToElement(element) {
      //暂时还用不了，
      if (element) {
        // 假设你有一个可以滚动的父元素和一个子元素
        const scrollableElement = document.querySelector(".resumeContainer");
        const distanceFromTop = element.offsetTop;
        // 如果滚动条不在顶部，考虑滚动条位置
        const actualDistanceFromTop =
          distanceFromTop - scrollableElement.scrollTop;
        console.log(
          "actualDistanceFromTop",
          actualDistanceFromTop,
          element.offsetTop
        );
        //滚动到元素位置
        scrollableElement.scrollTo({
          top: actualDistanceFromTop,
          behavior: "smooth",
        });
      }
    },
    expandContent(item, index) {
      this.$nextTick(() => {
        const element = this.$refs[`richContent${index}`][0];
        if (item.expendButtonText == "收起") {
          // 给元素移除expand类，以收起内容
          element.classList.remove("expand");
          this.$set(item, "expendButtonText", "展开");
        } else {
          // 给元素添加expand类，以展开内容
          element.classList.add("expand");
          this.$set(item, "expendButtonText", "收起");
        }
        console.log("element", element.classList);
      });
    },
    expandContentForSingle() {
      this.$nextTick(() => {
        const element = this.$refs[`richContent`];
        if (this.expendButtonText == "收起") {
          // 给元素移除expand类，以收起内容
          element.classList.remove("expand");
          this.$set(this, "expendButtonText", "展开");
        } else {
          // 给元素添加expand类，以展开内容
          element.classList.add("expand");
          this.$set(this, "expendButtonText", "收起");
        }
      });
    },
    isOverflowing(item, ref) {
      this.$nextTick(() => {
        const element = this.$refs[ref][0];
        // 检查元素内容是否超过82px（四行的高度是83px）
        this.$set(item, "showExpend", element.clientHeight > 82);
        this.$set(item, "expendButtonText", "展开");
      });
    },
    isOverflowingForSingle(item, ref) {
      this.$nextTick(() => {
        const element = this.$refs[ref];
        // 检查元素内容是否超过82px（四行的高度是83px）
        this.$set(item, "showExpend", element.clientHeight > 82);
        this.$set(item, "expendButtonText", "展开");
      });
    },
  },
};
